package com.right.health.app.utils;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

import com.magic.basic.utils.FileChooseUtil;
import com.right.health.app.widget.croper.CropImage;
import com.right.health.app.widget.croper.CropImageView;
import com.right.health.app.widget.croper.CroperUtil;

import java.io.File;

/*
 * Created by hopeliao on 2019-10-11
 *
 */
public class ChoosePhotoUtil {

    private Activity mAct;
    private FileChooseUtil mFileChooseUtil;

    private int mCropWidth, mCropHeight;

    private boolean mNeedCrop;

    private OnChooseImageCallback mCallback;

    public void setCropParam(int cropWidth, int cropHeight) {
        mCropWidth = cropWidth;
        mCropHeight = cropHeight;
        mNeedCrop = mCropWidth != 0 && mCropHeight != 0;
    }

    public ChoosePhotoUtil(Activity act) {
        mFileChooseUtil = new FileChooseUtil(act);
        mAct = act;
    }

    public void setCallback(OnChooseImageCallback callback) {
        this.mCallback = callback;
    }


    public void choosePhoto() {
        mFileChooseUtil.chooseFile(FileChooseUtil.TYPE_TAKE_PHOTO);
    }

    public void chooseAlbum() {
        mFileChooseUtil.chooseFile(FileChooseUtil.TYPE_ALBUM);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case FileChooseUtil.TYPE_ALBUM:
                    String mImagePath = mFileChooseUtil.resultFile(requestCode,
                            resultCode, data);

                    if (mNeedCrop) {
                        Uri imageUri = CropImage.getPickImageResultUri(mAct, data);

//                        CropImage.activity(imageUri)
//                                .setAutoZoomEnabled(true)  //裁剪过程可以拖动
//                                .setFixAspectRatio(true)
//                                .setMinCropResultSize(120, 120)  //裁剪最小位图大小（防止失真）
////                                .setAspectRatio(1, 1)  //裁剪比例  默认为不限制比例
//                                .setRequestedSize(mCropWidth, mCropHeight)  //裁剪输出路径
//                                .setGuidelines(CropImageView.Guidelines.ON_TOUCH)
//                                .start(mAct);

                        CropImage.activity(imageUri)
                                .setGuidelines(CropImageView.Guidelines.ON)
                                .setCropShape(CropImageView.CropShape.RECTANGLE)
                                .setRequestedSize(mCropWidth, mCropHeight)
                                .setMinCropResultSize(120, 120)
                                .start(mAct);

                    } else {
                        if (mCallback != null) {
                            mCallback.onResult(mImagePath);
                        }
                    }
                    break;
                case FileChooseUtil.TYPE_TAKE_PHOTO:
                    mImagePath = mFileChooseUtil.resultFile(requestCode,
                            resultCode, data);

                    if (mNeedCrop && !TextUtils.isEmpty(mImagePath)) {
                        Uri uri = CroperUtil.getImageContentUri(mAct, new File(mImagePath));
                        if (uri == null)
                            return;
                        CropImage.activity(uri)
                                .setGuidelines(CropImageView.Guidelines.ON)
                                .setCropShape(CropImageView.CropShape.RECTANGLE)
                                .setRequestedSize(mCropWidth, mCropHeight)
                                .setMinCropResultSize(120, 120)
                                .start(mAct);
                    } else {
                        if (mCallback != null) {
                            mCallback.onResult(mImagePath);
                        }
                    }
                    break;
                case CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE:
                    CropImage.ActivityResult result = CropImage.getActivityResult(data);
                    mImagePath = result.getUri().getPath();

                    if (mCallback != null) {
                        mCallback.onResult(mImagePath);
                    }
                    break;
            }
        }
    }

    public interface OnChooseImageCallback {
        void onResult(String filePath);
    }
}
